package com.kangyi.app.controller;

import com.kangyi.app.service.CharityService;
import com.kangyi.dto.ActivitySignupDTO;
import com.kangyi.dto.CharityListDTO;
import com.kangyi.result.PageResult;
import com.kangyi.result.Result;
import com.kangyi.vo.CharityDetailVO;
import com.kangyi.vo.PublicActivityDetailVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

/**
 * @author Blank
 * @version 1.0
 */
@RestController
@RequestMapping("/app/charity")
@Api(tags = "公益活动相关接口")
@Slf4j
public class CharityController {

    @Autowired
    private CharityService charityService;

    @GetMapping("/list")
    @ApiOperation("获取公益活动列表")
    public Result<PageResult> list(CharityListDTO charityListDTO){
        log.info("获取公益活动列表:{}", charityListDTO);
        try {
            PageResult pageResult = charityService.list(charityListDTO);
            return Result.success(pageResult,"查询成功！");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    @GetMapping("/detail/{id}")
    @ApiOperation("公益活动详情")
    @Cacheable(cacheNames = "publicActivity_" , key = "'userDetail_'+#id")
    public Result<CharityDetailVO> detailPublicActivity(@PathVariable Integer id){
        log.info("公益活动详情:{}",id);
        try {
            CharityDetailVO charityDetailVO = charityService.detailPublicActivity(id);
            return Result.success(charityDetailVO,"查询成功");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    @PostMapping("/register")
    @ApiOperation("公益活动报名")
    public Result register(@RequestBody ActivitySignupDTO activitySignupDTO){
        log.info("公益活动报名:{}",activitySignupDTO);
        try {
            charityService.register(activitySignupDTO);
            return Result.success("报名成功");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    @DeleteMapping("/cancelRegistration/{registrationId}")
    public Result cancelRegistration(@PathVariable Integer registrationId){
        log.info("取消公益活动报名：{}",registrationId);
        try {
            charityService.cancelRegistration(registrationId);
            return Result.success("取消成功！");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
}
